package com.chinafree.permission.controller;

import com.chinafree.common.model.enumeration.ResponseCodeEnum;
import com.chinafree.common.model.request.RequestParameter;
import com.chinafree.common.model.response.EntityResponse;
import com.chinafree.common.model.response.ListResponse;
import com.chinafree.permission.converter.RoleConverter;
import com.chinafree.permission.converter.RolePermissionConverter;
import com.chinafree.permission.model.bo.RoleBO;
import com.chinafree.permission.model.bo.RoleDepartUserEditBO;
import com.chinafree.permission.model.bo.RolePermissionEditBO;
import com.chinafree.permission.model.dto.PermissionDTO;
import com.chinafree.permission.model.dto.RoleDTO;
import com.chinafree.permission.model.dto.RoleInfoDTO;
import com.chinafree.permission.model.enumeration.PermissionTypeEnum;
import com.chinafree.permission.model.param.RoleDepartUserEditParam;
import com.chinafree.permission.model.param.RoleParam;
import com.chinafree.permission.model.param.RolePermissionEditParam;
import com.chinafree.permission.service.RolePermissionService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * @description:
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/7/22 15:11
 * @version:v1.0
 */
@Slf4j
@Validated
@RestController
@Api(tags = "权限_角色权限")
public class RolePermissionController {
//
//    @Autowired
//    private WxApiService wxApiService;

    @Autowired
    private RolePermissionService rolePermissionService;

//    @Autowired
//    private PathServie pathServie;

    /**
     * @description: 获取所有角色列表
     * @author longzix
     * @date 2021/7/25 20:15
     * @return com.xinheyun.model.response.EntityResponse<java.util.List<com.xinheyun.operation.auth.dto.RoleDTO>>
     */
    @PostMapping("/permission/getRoleInfo")
    public ListResponse<RoleDTO> getRoleList() {
        Optional<List<RoleDTO>> roleList = rolePermissionService.getRoleList();
        return roleList.map(roleDTOS -> new ListResponse<>(ResponseCodeEnum.SUCCESS, "查询成功", roleDTOS))
            .orElseGet(() -> new ListResponse<>(ResponseCodeEnum.NOT_FOUND, "查询失败", null));
    }

    /**
     * @description: 根据角色id 获取角色的基本信息，以及对应的用户列表，部门列表，权限列表
     * @date 2021/7/25 21:46
     * @return com.xinheyun.operation.auth.dto.RoleInfoDTO
     */
    @PostMapping("/permission/getAllDepartmentUser")
    public EntityResponse<RoleInfoDTO> getRoleInfo(RequestParameter<Integer> roleId) {
        Optional<RoleInfoDTO> roleInfo = rolePermissionService.getRoleInfo(roleId.getBody());
        return roleInfo.map(roleInfoDTO -> new EntityResponse<>(ResponseCodeEnum.SUCCESS, "查询成功", roleInfoDTO))
            .orElseGet(() -> new EntityResponse<>(ResponseCodeEnum.NOT_FOUND, "查询失败", null));
    }


    /**
     * @description: 查询所有权限列表，按照权限类型划分
     * @author longzix
     * @date 2021/7/25 20:48
     * @return com.xinheyun.model.response.EntityResponse<java.util.Map<com.xinheyun.operation.auth.model.enumeration.PermissionTypeEnum,java.util.List<com.xinheyun.operation.auth.dto.PermissionDTO>>>
     */
    @PostMapping("/permission/queryPermissionList")
    public EntityResponse<Map<PermissionTypeEnum, List<PermissionDTO>>> getPermissionList() {
        Optional<Map<PermissionTypeEnum, List<PermissionDTO>>> permissionList = rolePermissionService.getPermissionList();
        return permissionList.map(permissionTypeEnumListMap -> new EntityResponse<>(ResponseCodeEnum.SUCCESS, "查询成功", permissionTypeEnumListMap))
            .orElseGet(() -> new EntityResponse<>(ResponseCodeEnum.NOT_FOUND, "查询失败", null));
    }

    /**
     * @description: 创建一个角色，并且为该角色分配，用户，部门，以及权限
     * @param
     * @author longzix
     * @date 2021/7/23 13:06
     * @return com.xinheyun.model.response.BaseResponse
     */
    @PostMapping("/permission/createRole")
    public EntityResponse<Integer> createRole(RequestParameter<RoleParam> requestParameterRoleRaram) {
        RoleParam roleParam = requestParameterRoleRaram.getBody();
        if(StringUtils.isEmpty(roleParam.getRoleName())){//角色名不能为空字符串
            return new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "创建失败，角色名不能为空", null);
        }
        RoleBO roleBO = RoleConverter.INSTANCE.RoleParamToBO(roleParam);
        Optional<Integer> optRoleId = rolePermissionService.createRole(roleBO);
        return optRoleId.map(integer -> new EntityResponse<>(ResponseCodeEnum.SUCCESS, "创建成功", integer))
            .orElseGet(() -> new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "创建失败", null));
    }

    /**
     * @description: 编辑角色 与权限关联关系
     * @param
     * @author longzix
     * @date 2021/7/28 18:25
     * @return com.xinheyun.model.response.EntityResponse<java.lang.Integer>
     */
    @PostMapping("/permission/editRolePermissionInfo")
    public EntityResponse<Integer> editRolePermissionInfo(RequestParameter<RolePermissionEditParam> requestRolePermissionParam) {
        RolePermissionEditParam rolePermissionParam = requestRolePermissionParam.getBody();
        RolePermissionEditBO rolePermissionEditBO = RolePermissionConverter.INSTANCE.rolePermissionParamToEditBO(rolePermissionParam);
        Optional<Integer> optRoleId = rolePermissionService.editRolePermission(rolePermissionEditBO);
        return optRoleId.map(integer -> new EntityResponse<>(ResponseCodeEnum.SUCCESS, "修改成功", integer))
            .orElseGet(() -> new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "修改失败", null));
    }

    /**
     * @description: 编辑角色与 用户部门关联关系
     * @param requestRoleUserAndDepartParam
     * @author longzix
     * @date 2021/7/28 18:26
     * @return com.xinheyun.model.response.EntityResponse<java.lang.Integer>
     */
    @PostMapping("/permission/editRoleUserAndDepartInfo")
    public EntityResponse<Integer> editRoleUserAndDepartInfo(RequestParameter<RoleDepartUserEditParam> requestRoleUserAndDepartParam) {
        RoleDepartUserEditParam roleUserAndDepartParam = requestRoleUserAndDepartParam.getBody();
        RoleDepartUserEditBO roleDepartUserEditBO = RolePermissionConverter.INSTANCE.roleUserAndDepartParamToEditBO(roleUserAndDepartParam);
        Optional<Integer> optRoleId = rolePermissionService.editRoleDepartUSer(roleDepartUserEditBO);
        return optRoleId.map(integer -> new EntityResponse<>(ResponseCodeEnum.SUCCESS, "修改成功", integer))
            .orElseGet(() -> new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "修改失败", null));
    }

    /* *
     * @description: 编辑角色基本信息
     * @param roleParam
     * @author longzix
     * @date 2021/7/28 18:25
     * @return com.xinheyun.model.response.EntityResponse<java.lang.Integer>
     */
    @PostMapping("/permission/editRole")
    public EntityResponse<Integer> editRole(RequestParameter<RoleParam> requestRoleParam) {
        RoleParam roleParam = requestRoleParam.getBody();
        Optional<Integer> optId = rolePermissionService.editRole(roleParam);
        return optId.map(integer -> new EntityResponse<>(ResponseCodeEnum.SUCCESS, "修改成功", integer))
            .orElseGet(() -> new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "修改失败", null));
    }

    /* *
     * @description: 删除角色信息，包括关联的用户，部门，权限
     * @param roleId
     * @author longzix
     * @date 2021/7/25 22:08
     * @return java.lang.Integer 返回受影响的数据条数
     */
    @PostMapping("/permission/deleteRole")
    public EntityResponse<Integer> deleteRole(RequestParameter<Integer> roleId) {
        Optional<Integer> optRoleId = rolePermissionService.deleteRole(roleId.getBody());
        return optRoleId.map(integer -> new EntityResponse<>(ResponseCodeEnum.SUCCESS, "删除成功", integer))
            .orElseGet(() -> new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "删除失败", null));
    }


}